package com.bilibili.server.controller;

import com.bilibili.server.dto.Response;
import com.bilibili.server.entity.Comment;
import com.bilibili.server.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/interactions/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

    // 获取视频评论列表
    @GetMapping("/video/{videoId}")
    public ResponseEntity<Response<Page<Comment>>> getVideoComments(
            @PathVariable Long videoId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            Page<Comment> comments = commentService.getVideoComments(videoId, page, pageSize);
            return new ResponseEntity<>(Response.success("获取成功", comments), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 获取子评论列表
    @GetMapping("/video/{videoId}/replies/{parentId}")
    public ResponseEntity<Response<Page<Comment>>> getChildComments(
            @PathVariable Long videoId,
            @PathVariable int parentId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            Page<Comment> comments = commentService.getChildComments(videoId, parentId, page, pageSize);
            return new ResponseEntity<>(Response.success("获取成功", comments), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 创建评论
    @PostMapping
    public ResponseEntity<Response<Comment>> createComment(@RequestBody Map<String, Object> commentData) {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String username = authentication.getName();

            Integer videoIdInt = (Integer) commentData.get("videoId");
            Integer parentId = (Integer) commentData.get("parentId");
            String content = (String) commentData.get("content");

            if (videoIdInt == null || content == null || content.trim().isEmpty()) {
                return new ResponseEntity<>(Response.error(400, "视频ID和评论内容不能为空"), HttpStatus.BAD_REQUEST);
            }

            // 将Integer转换为Long
            Long videoId = videoIdInt.longValue();
            Comment comment = commentService.createComment(videoId, parentId, content, username);
            return new ResponseEntity<>(Response.created("创建成功", comment), HttpStatus.CREATED);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(Response.error(400, e.getMessage()), HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 删除评论
    @DeleteMapping("/{commentId}")
    public ResponseEntity<Response<Void>> deleteComment(@PathVariable int commentId) {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String username = authentication.getName();

            commentService.deleteComment(commentId, username);
            return new ResponseEntity<>(Response.success("删除成功"), HttpStatus.OK);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(Response.error(400, e.getMessage()), HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 点赞评论
    @PostMapping("/{commentId}/like")
    public ResponseEntity<Response<Void>> likeComment(@PathVariable int commentId) {
        try {
            commentService.likeComment(commentId);
            return new ResponseEntity<>(Response.success("点赞成功"), HttpStatus.OK);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(Response.error(400, e.getMessage()), HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}